/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.ContaCor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rafael Kozar
 */
public class ContaCorrenteDAO {
     private Connection connection;     
     
     public ContaCorrenteDAO(){
         this.connection = new ConnectionFactory().getConnection();
     }
     
     public void insere(int codCliente, ContaCor corrente) throws SQLException {         
         String sql1 = "INSERT INTO conta (idCliente, saldo )  VALUES (?, ?)";
         String sql2 = "INSERT INTO contacorrente(depositoInicial, limite, fkconta) values(?, ?, ?) ";
         String sql3 = "SELECT numConta FROM CONTA WHERE idCliente =" + codCliente + "";
         PreparedStatement stmt1 = connection.prepareStatement(sql1);
         PreparedStatement stmt2 = connection.prepareStatement(sql2);
         PreparedStatement stmt3 = connection.prepareStatement(sql3);
         System.out.println(codCliente);
         stmt1.setInt(1, codCliente);
         stmt1.setDouble(2, corrente.getDepositoInicial());
         stmt1.executeUpdate();
         ResultSet rs = stmt3.executeQuery();         
         int fkconta = 0;
         while(rs != null && rs.next()) fkconta = rs.getInt("numConta");
         stmt2.setDouble(1, corrente.getDepositoInicial());
         stmt2.setDouble(2, corrente.getLimite());
         stmt2.setInt(3, fkconta);
         stmt2.executeUpdate();
        
     }
     
     public void saca(Double valor, int idCliente) throws SQLException {
        String sql = "UPDATE conta SET saldo = (saldo-?) WHERE idCliente = ?";
        PreparedStatement stmt1 = connection.prepareStatement(sql);
        stmt1.setDouble(1, valor);
        stmt1.setInt(2, idCliente);
        stmt1.executeUpdate();
        //return true;
    }
             
}
